import 'package:flutter/material.dart';
import 'package:flutter2/colors.dart';
import 'package:flutter2/http/api.dart';
import 'package:flutter2/http/dio_manager.dart';
import 'package:flutter2/model/person_info_entity.dart';
import 'package:flutter2/model/sheet_item_entity.dart';
import 'package:flutter2/ui/contact.dart';
import 'package:flutter2/ui/sheet.dart';
import 'package:flutter2/widget/base/normal_button.dart';
import 'package:flutter2/widget/info_bar.dart';
import 'package:flutter2/widget/step_view.dart';
import 'package:flutter2/widget/title_bar.dart';
import 'package:flutter2/widget/top_tip.dart';
import 'package:get/get.dart';
import 'package:flutter2/utils/string.dart';

class WorkPage extends StatefulWidget {
  const WorkPage({super.key});

  @override
  State<WorkPage> createState() => _WorkPageState();
}

class _WorkPageState extends State<WorkPage> {
  final List<String> _list = ["hideclonesuspect", "invitenightpint", "schoolmatejuly", "cocoaundo"];
  final Map<String?, SheetItemEntity?> _map = {};
  final Map<String?, bool> _clicks = {};

  @override
  void initState() {
    super.initState();
    DioManager.request<PersonInfoEntity>(
      path: HttpApi.getInfo,
      onSuccess: (data) {
        if (data == null) return;
        _map[_list[0]] = SheetItemEntity.from(data.live, data.liveDesc);
        _map[_list[1]] = SheetItemEntity.from(data.edu, data.eduDesc);
        _map[_list[2]] = SheetItemEntity.from(data.workYear, data.workYearDesc);
        _map[_list[3]] = SheetItemEntity.from(data.work, data.workDesc);
        setState(() {});
      },
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: mainBg,
      appBar: TitleBar.buildAppBar(
        title: "基本信息",
        back: () => Get.back(),
      ),
      body: Column(children: [
        Padding(
          child: StepView(max: 7, step: 2),
          padding: EdgeInsets.only(left: 16, right: 16, top: 10),
        ),
        Expanded(
          child: SingleChildScrollView(
            physics: BouncingScrollPhysics(),
            child: Column(
              children: [
                TopTip(title: "基本信息"),
                ...buildInfo(context),
              ],
            ),
          ),
        ),
        NormalButton(
          click: upload,
          enable: isEnable(),
          width: double.infinity,
          margin: EdgeInsets.symmetric(horizontal: 16, vertical: 20),
          text: "确定",
        ),
      ]),
    );
  }

  List<InfoBar> buildInfo(BuildContext context) {
    List<InfoBar> list = [];
    for (int i = 0; i < _list.length; i++) {
      String title = "";
      if (i == 0) {
        title = "居住类型";
      } else if (i == 1) {
        title = "学历";
      } else if (i == 2) {
        title = "工作年限";
      } else {
        title = "工作";
      }
      list.add(InfoBar(
        title: title,
        value: _map[_list[i]]?.value,
        code: _map[_list[i]]?.code,
        click: () {
          autoShow(_list[i]);
        },
      ));
    }
    return list;
  }

  autoShow(String? type) async {
    await Future.delayed(Duration.zero);
    if (!mounted) return;
    type ??= _list.where((element) => _map[element]?.isNotNull() != true).firstOrNull;
    if (type.isNull()) {
      if (_list.every((element) => _clicks[element] == true)) {
        upload();
      }
      return;
    }
    _clicks[type] = true;
    SheetData.getSheetData(
      type: type,
      call: (item) {
        _map[type] = item;
        setState(() {});
        autoShow(null);
      },
    );
  }

  upload() {
    var param = getParams();
    for (var element in _list) {
      param[element] = _map[element]?.code;
    }
    DioManager.request(
      path: HttpApi.saveInfo,
      data: param,
      onSuccess: (data) {
        Get.off(ContactPage());
      },
    );
  }

  bool isEnable() {
    return _list.every((element) {
      SheetItemEntity? item = _map[element];
      return item?.isNotNull() == true;
    });
  }
}
